package com.zhiche.lisa.bms.controller.fee;


import com.baomidou.mybatisplus.plugins.Page;
import com.zhiche.lisa.bms.controller.utils.ExcelController;
import com.zhiche.lisa.bms.dao.model.fee.FeeAssessment;
import com.zhiche.lisa.bms.dao.model.price.PriceIncoming;
import com.zhiche.lisa.bms.pojo.vo.common.CommonVO;
import com.zhiche.lisa.bms.pojo.vo.fee.FeeAssessmentVO;
import com.zhiche.lisa.bms.service.fee.FeeAssessmentService;
import com.zhiche.lisa.core.enums.CommonEnum;
import com.zhiche.lisa.core.supports.BaseException;
import com.zhiche.lisa.core.supports.RestfulResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * <p>
 * 考核奖惩费用 前端控制器
 * </p>
 *
 * @author WangShuiJin
 * @since 2018-09-20
 */
@RequestMapping("/feeAssessment")
@Api(value = "/feeAssessment", description = "考核奖惩接口", tags = {"考核奖惩接口"})
@RestController
public class FeeAssessmentController {
    private static final Logger LOGGER = LoggerFactory.getLogger(ExcelController.class);

    @Autowired
    private FeeAssessmentService feeAssessmentService;

    @PostMapping(value = "/queryFeeAssessmentPage")
    @ApiOperation(value = "查询考核奖惩费用列表（分页）", notes = "查询考核奖惩费用列表（分页）", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<Page<FeeAssessment>> queryFeeAssessmentPage(@RequestBody Page<FeeAssessment> page) {
        RestfulResponse<Page<FeeAssessment>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            Page<FeeAssessment> resultData = feeAssessmentService.queryFeeAssessmentPage(page);
            result.setData(resultData);
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.queryFeeAssessmentPage ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.queryFeeAssessmentPage ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/getFeeAssessmentByPayIdList")
    @ApiOperation(value = "根据付款申请ID，查询考核奖惩费用列表", notes = "根据付款申请ID，查询考核奖惩费用列表", httpMethod = "GET")
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<FeeAssessment>> getFeeAssessmentByPayIdList(@RequestParam Long payId) {
        RestfulResponse<List<FeeAssessment>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<FeeAssessment> resultData = feeAssessmentService.getFeeAssessmentByPayIdList(payId);
            result.setData(resultData);
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.getFeeAssessmentByPayIdList ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.getFeeAssessmentByPayIdList ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping(value = "/getFeeAssessmentByFleetIdList")
    @ApiOperation(value = "根据承运人,查询考核奖惩费用列表", notes = "根据承运人,查询考核奖惩费用列表", httpMethod = "GET")
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<FeeAssessment>> getFeeAssessmentByFleetIdList(@RequestParam Long payeeId, @RequestParam Integer billProperty) {
        RestfulResponse<List<FeeAssessment>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<FeeAssessment> resultData = feeAssessmentService.getFeeAssessmentByFleetIdList(payeeId, billProperty);
            result.setData(resultData);
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.getFeeAssessmentByFleetIdList ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.getFeeAssessmentByFleetIdList ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/getAvailableFeeAssessment")
    @ApiOperation(value = "根据承运人,查询考核奖惩费用列表", notes = "根据承运人,查询考核奖惩费用列表", httpMethod = "POST")
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<FeeAssessment>> getAvailableFeeAssessment(@RequestBody Map<String, Object> condition) {
        RestfulResponse<List<FeeAssessment>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<FeeAssessment> resultData = feeAssessmentService.getAvailableFeeAssessment(condition);
            result.setData(resultData);
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.getAvailableFeeAssessment ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.getAvailableFeeAssessment ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/blockedFeeAssessment")
    @ApiOperation(value = "考核单审核", notes = "考核单审核", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<List<String>> blockedFeeAssessment(@RequestBody List<Long> ids, @RequestHeader("Authorization") String token) {
        RestfulResponse<List<String>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            List<String> strings = feeAssessmentService.blockedFeeAssessment(ids, token);
            if (!CollectionUtils.isEmpty(strings)) {
                result.setCode(-2);
                result.setData(strings);
            }
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.blockedFeeAssessment ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.blockedFeeAssessment ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/publishFeeAssessment")
    @ApiOperation(value = "考核单发布", notes = "考核单发布", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> publishFeeAssessment(@RequestBody List<FeeAssessment> feeAssessmentList) {
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            feeAssessmentService.publishFeeAssessment(feeAssessmentList);
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.publishFeeAssessment ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.publishFeeAssessment ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @PostMapping(value = "/deleteFeeAssessment")
    @ApiOperation(value = "删除考核奖惩费用", notes = "根据考核单号，删除考核奖惩费用", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiResponses({@ApiResponse(code = 0, message = "success", response = PriceIncoming.class), @ApiResponse(code = -1, message = "系统异常")})
    public RestfulResponse<String> deleteFeeAssessment(@RequestBody List<Long> assessCodeList) {
        RestfulResponse<String> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), CommonEnum.SUCCESS.getText(), null);
        try {
            feeAssessmentService.deleteFeeAssessment(assessCodeList);
        } catch (BaseException e) {
            LOGGER.error("/BillIoController.deleteFeeAssessment ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), e.getMessage(), null);
        } catch (Exception e) {
            LOGGER.error("/BillIoController.deleteFeeAssessment ERROR: {}", e.getMessage());
            result = new RestfulResponse(CommonEnum.ERROR.getCode(), CommonEnum.ERROR.getText(), null);
        }
        return result;
    }

    @GetMapping("/getFeeAssessListByPayId/{payId}")
    @ApiOperation(value = "考核单费用获取", notes = "考核单费用获取", httpMethod = "GET", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<List<FeeAssessmentVO>> getFeeAssessListByPayId(@PathVariable("payId") Long payId) {
        LOGGER.info("FeeAssessmentController.getFeeAssessListByPayId payId: {}", payId);
        RestfulResponse<List<FeeAssessmentVO>> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "success", null);
        try {
            result.setData(feeAssessmentService.getFeeAssessmentVoListByPayId(payId));
        } catch (BaseException e) {
            LOGGER.error("FeeAssessmentController.getFeeAssessListByPayId error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeAssessmentController.getFeeAssessListByPayId error:{}", e.getMessage());
            result.setMessage("获取考核费用发布异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }


    @PostMapping(value = "/closeAssessment")
    @ApiOperation(value = "考核单的关闭", notes = "考核单的关闭", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    public RestfulResponse<Object> closeAssessment(@RequestBody CommonVO<Long> feeRewardCommonVO) {
        LOGGER.info("FeeRewardController.release param: {}", feeRewardCommonVO);
        RestfulResponse<Object> result = new RestfulResponse<>(CommonEnum.SUCCESS.getCode(), "关闭成功", null);
        try {
            if (Objects.isNull(feeRewardCommonVO)) {
                throw new BaseException("请选择数据");
            }
            if (StringUtils.isBlank(feeRewardCommonVO.getTypeVal())) {
                throw new BaseException("请输入关闭原因");
            }
            if (CollectionUtils.isEmpty(feeRewardCommonVO.getEntityParams())) {
                throw new BaseException("请选择数据");
            }
            List<String> resultMessageList = feeAssessmentService.closeAssessment(feeRewardCommonVO.getEntityParams(), feeRewardCommonVO.getTypeVal());
            if (!CollectionUtils.isEmpty(resultMessageList)) {
                result.setMessage("失败的数据");
                result.setData(resultMessageList);
                result.setCode(-2);
            }
        } catch (BaseException e) {
            LOGGER.error("FeeRewardController.closeAssessment error:{}", e.getMessage());
            result.setMessage(e.getMessage());
            result.setCode(CommonEnum.ERROR.getCode());
        } catch (Exception e) {
            LOGGER.error("FeeRewardController.closeAssessment error:{}", e.getMessage());
            result.setMessage("考核单的关闭异常");
            result.setCode(CommonEnum.ERROR.getCode());
        }
        return result;
    }
}
